package cn.vc.media.controller;

import cn.vc.auth.entity.po.VUser;
import cn.vc.basicmodule.entity.Result;
import cn.vc.media.service.BigFileService;
import cn.vc.media.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @author Mr.M
 * @version 1.0
 * @description 上传视频
 * @date 2023/2/18 10:34
 */

@Api(value = "大文件上传接口", tags = "大文件上传接口")
@RestController
@RequestMapping("/uploadbigfile")
//@PreAuthorize("hasAnyAuthority('ordinary')")
public class BigFilesController {

    @Autowired
    private BigFileService bigFileService;

    @ApiOperation(value = "文件上传前检查文件")
    @PostMapping("/checkfile")
    public Result<?> checkfile(@RequestParam("fileMd5") String fileMd5) {
        Result<?> result = bigFileService.checkFile(fileMd5);
        return result;
    }

    @ApiOperation(value = "分块文件上传前的检测")
    @PostMapping("/checkchunk")
    public Result<?> checkChunk(@RequestParam("fileMd5") String fileMd5, @RequestParam("chunk") int chunk) throws Exception {
        boolean bo = bigFileService.checkChunk(fileMd5, chunk);
        return new Result<>(bo, chunk, null);
    }

    @ApiOperation(value = "上传分块文件")
    @PostMapping("/uploadchunk")
    public Result<?> uploadChunk(@RequestParam("fileMd5") String fileMd5, @RequestParam("chunk") int chunk,
                                 @RequestPart("file") MultipartFile file) throws Exception {
        //创建一个临时文件
        File tempFile = File.createTempFile("minio", ".temp");
        file.transferTo(tempFile);
        //文件路径
        String localFilePath = tempFile.getAbsolutePath();

        Result<?> result = bigFileService.uploadChunk(fileMd5, chunk, localFilePath);

        return result;
    }

    /**
     *
     * @param fileMd5
     * @param fileName 源文件名称
     * @param chunkTotal
     */
    @ApiOperation(value = "合并文件")
    @PostMapping("/mergechunks")
    public Result<?> mergeChunks(@RequestParam("fileMd5") String fileMd5, @RequestParam("fileName") String fileName, @RequestParam("chunkTotal") int chunkTotal) {
        Result<?> result = bigFileService.mergechunks(fileMd5, chunkTotal, fileName);
        return result;
    }


}
